package com.hp.service;

import com.hp.dao.*;
import com.hp.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class AgentService {

    @Autowired
    private AgentVerificationMapper agentVerificationMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SellMapper sellMapper;
    @Autowired
    private RentingMapper rentingMapper;
    @Autowired
    private HouseMapper houseMapper;

    public HashMap<String, Object> selectAgentVerifications(int userId) {

        HashMap<String, Object> res = new HashMap<>();

        List<AgentVerification> agentVerifications = agentVerificationMapper.selectAllByUserId(userId);
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        agentVerifications.forEach(v -> {
            HashMap<String, Object> map = new HashMap<>();

            map.put("fromId", v.getFromId());
            map.put("toId", v.getToId());
            map.put("houseId", v.getHouseId());
            map.put("verifyStatus", v.getVerifyStatus());
            map.put("verifyTime", v.getVerifyTime());
            User user = null;
            if (v.getUserId() == v.getFromId()) {
                user = userMapper.getUserById(v.getToId());
            } else {
                user = userMapper.getUserById(v.getFromId());
            }
            map.put("nickname", user.getUserNickname());
            map.put("headPhoto", user.getUserPhoto());
            map.put("houseSellOrRent", getHouseSellOrRent(v.getHouseId()));
            list.add(map);
        });

        res.put("agentVerifications", list);
        res.put("status", 1);
        res.put("msg", "获取成功！");

        return res;

    }

    public int getHouseSellOrRent(int houseId) {
        // 0.sell 1.rent 2.neither
        Sell sell = sellMapper.getSellByHouseId(houseId);
        if (sell != null) {
            return 0;
        }
        Renting rent = rentingMapper.getRentingByHouseId(houseId);
        if (rent != null) {
            return 1;
        }
        return 2;
    }

    public HashMap<String, Object> selectAgentHouses(int userId) {

        HashMap<String, Object> res = new HashMap<>();

        ArrayList<HashMap<String, Object>> passhouse = new ArrayList<>();

        ArrayList<HashMap<String, Object>> nopasshouse = new ArrayList<>();

        List<House> houses = houseMapper.getHouseByUserId(userId);
        houses.forEach(house -> {
            HashMap<String, Object> map = new HashMap<>();

            map.put("houseId", house.getHouseId());
            map.put("houseDetailaddress", house.getHouseDetailaddress());
            int houseSellOrRent = getHouseSellOrRent(house.getHouseId());
            map.put("sellOrRent", getHouseSellOrRent(house.getHouseId()));
            boolean agent = false;
            if (houseSellOrRent == 0) {
                Sell sell = sellMapper.getSellByHouseId(house.getHouseId());
                if (sell.getAgentId() > 0) {
                    agent = true;
                }
            } else if (houseSellOrRent == 1) {
                Renting rent = rentingMapper.getRentingByHouseId(house.getHouseId());
                if (rent.getAgentId() > 0) {
                    agent = true;
                }
            }

            if (agent) {
                passhouse.add(map);
            } else {
                nopasshouse.add(map);
            }
        });

        res.put("status", 1);
        res.put("passhouse", passhouse);
        res.put("nopasshouse", nopasshouse);
        res.put("msg", "获取成功！");

        return res;

    }
}
